package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.exception.CustException;
import com.heima.model.admin.pojos.AdUser;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmUserDto;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.HashMap;

@Service
public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {
    /**
     * 登录
     *
     * @param wmUserDto
     * @return
     */
    @Override
    public ResponseResult login(WmUserDto wmUserDto) {
        //参数校验
        if (StringUtils.isBlank(wmUserDto.getName()) || StringUtils.isBlank(wmUserDto.getPassword())) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_NAMEORPSW_ISNOTNULL);
        }

        //查询用户名
        WmUser wmUser = getOne(Wrappers.<WmUser>lambdaQuery().
                eq(WmUser::getName, wmUserDto.getName())
                .eq(WmUser::getStatus, 9));

        if (wmUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_NAMENOTNORMALORPSW_ERROR);
        }

        //比对密码
        String dbpsw = wmUser.getPassword();
        String psw = wmUserDto.getPassword();
        String salt = wmUser.getSalt();
        String npsw = psw + salt;
        String dpsw = DigestUtils.md5DigestAsHex(npsw.getBytes());
        if (!dbpsw.equals(dpsw)) {
            CustException.cust(AppHttpCodeEnum.ADMIN_NAMEORPSW_ERROR);
        }

        //设置最后一次登录时间
        wmUser.setLoginTime(new Date());
        updateById(wmUser);

        //根据用户名拿到token,把用户信息存在token中并返回给前端
        String token = AppJwtUtil.getToken(wmUser.getId().longValue());
        HashMap<String, Object> map = new HashMap<>();
        map.put("token", token);
        wmUser.setPassword(null);
        wmUser.setPhone(null);
        wmUser.setSalt(null);

        map.put("user", wmUser);

        return ResponseResult.okResult(map);
    }

    /**
     * 注册
     *
     * @param wmUser
     * @return
     */
    @Override
    public ResponseResult regist(WmUser wmUser) {
        //参数校验
//        if (StringUtils.isBlank(adUser.getName()) || StringUtils.isBlank(adUser.getPassword())) {
//            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_NAMEORPSW_ISNOTNULL);
//        }

        //注册用户信息
        //判断用户名是否重复
        WmUser wmUser1 = getOne(Wrappers.<WmUser>lambdaQuery()
                .eq(WmUser::getName, wmUser.getName())
        );
        if (wmUser1 != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_NAME_EXIST);
        }

        //获取密码加密加盐
        wmUser.setSalt("123abc");
        String psw = wmUser.getPassword();
        String salt = wmUser.getSalt();
        String npsw = DigestUtils.md5DigestAsHex((psw + salt).getBytes());

        wmUser.setPassword(npsw);
        wmUser.setCreatedTime(new Date());
        wmUser.setStatus(9);

        save(wmUser);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
